package system;

import java.io.File;
import java.io.IOException;
import java.util.*;

public class Director {

	ArrayList<Actor> actorList; 	
	private final int maxActors = 100; // maximum number of actors a director can handle
	
	// Particle Constants
	public final int EXPLOSION_LG = 0;
	public final int EXPLOSION_SM = 1;
	public final int SPARKLE = 2;
	
	public Director(){
		actorList = new ArrayList<Actor>();
	}
	
	public void add(Actor anActor){
		if (actorList.size() < maxActors){
			actorList.add(anActor);
		}
	}
		
	public void addParticle(int x, int y, int particleType) {
		try {
			Actor particle;
			switch(particleType){			
			case EXPLOSION_SM:				
				particle = new Actor("Explosion", new AnimatedSprite(new File("assets/sprites/explosion-sm.png"), 12, 12, false, 12), x, y, 0, 0, 0, 8);
				break;
			case EXPLOSION_LG:
				particle = new Actor("Explosion", new AnimatedSprite(new File("assets/sprites/explosion-lg.png"), 30, 30, false, 12), x, y, 0, 0, 0, 8);
				break;
			case SPARKLE:
				particle = new Actor("Sparkle", new AnimatedSprite(new File("assets/sprites/sparkle.png"), 50, 50, false, 30), x, y, 0, 0, 0, 10);
				break;
			default:
				// this shouldn't ever execute
				System.out.println("warning, unhandled case in director AddExplosion");
				particle = new Actor("Explosion", new AnimatedSprite(new File("assets/sprites/explosion-lg.png"), 30, 30, false, 12), x, y, 0, 0, 0);
			} 
			add(particle);
		} catch (Exception e){
			System.out.println("could not load explosion sprite.");
		}
		
	}
	
	public void addBullet(Actor p, float dx, float dy){
		int x = p.getX() + 58;
		int y = p.getY() + 22;

		try {
			AnimatedSprite bulletSprite = new AnimatedSprite(new File("assets/sprites/bullet.png"), 5, 5, true, 34);
			if(p.getPower() > 30){
				bulletSprite = new AnimatedSprite(new File("assets/sprites/powerup-b.png"), 16, 16, true, 34);
			}
			
			Actor bullet = new Actor("Bullet", bulletSprite, x, y, dx, dy, p.getPower());
			add(bullet);
		} catch (Exception e){
			System.out.println("could not load bullet sprite.");
		}
	}
	
	public void addEnemyBullet(Actor p, float dx, float dy){
		int x = p.getX()+17;
		int y = p.getY()+32;
		
		try{
			AnimatedSprite bulletSprite = new AnimatedSprite(new File("assets/sprites/bullet.png"),5,5,true,34);
			Actor bullet = new Actor("Bullet", bulletSprite, x, y, dx, dy, p.getPower());
			add(bullet);
		} catch(Exception e){
			System.out.println("could not load bullet sprite.");
		}
		
		
	}
	
	public void addCoin(){
		
		float x = (float) Math.random() * (480-20) + 10;
		float y = Math.random() > 0.5 ? -20 : 270 + 20;
		
		float dx = 0;
		float dy = (float) (y > 0 ? -(Math.random()*2+0.5) : (Math.random()*2+0.5));
		
		try {
			Actor coin = new Actor("Coin", new AnimatedSprite(new File("assets/sprites/coin.png"), 14, 14, true, 60), x, y, dx, dy, 0);
			coin.setHp(1);
			add(coin);
		} catch (Exception e){
			System.out.println("could not load coin sprite.");
		}
		
	}
	
	public void addPowerup(Actor actor, float x, float y){
		
		try {
			int type = (int) Math.ceil((Math.random() * (2)));
			Actor powerUp = new PowerUp(actor, x, y, type);
			add(powerUp);
		} catch (Exception e){
			System.out.println("could not load powerUp sprite.");
		}
		
	}
	
	public void addAsteroid(){
		float x = 480f;
		float y = (float) Math.round(220 * Math.random());
		
		float dx = (float) -(Math.random()*4 + 1);
		float dy = (float) (Math.random() - 0.5);
		
		int spinSpeed = (int) Math.random()*1000 + 100;
		
		try {
			
			int chance = (int)Math.round(Math.random()*100);
			if(chance <= 50){
				Actor smallasteroid = new Actor("Asteroid", new AnimatedSprite(new File("assets/sprites/asteroid.png"),25,26,true, spinSpeed), x, y, dx, dy, 4);
				smallasteroid.setHp(20);
				add(smallasteroid);
			}
			else if(chance <= 80){
				Actor medasteroid = new Actor("Asteroid", new AnimatedSprite(new File("assets/sprites/asteroid-3.png"), 28,28, true, spinSpeed),x,y,dx,dy,10);
				medasteroid.setHp(50);
				add(medasteroid);
			}
			else{
				Actor bigasteroid = new Actor("Asteroid", new AnimatedSprite(new File("assets/sprites/asteroid-2.png"), 40,40,true,spinSpeed),x,y,dx,dy,16);
				bigasteroid.setHp(100);
				add(bigasteroid);
			}
			
			
		} catch (IOException e) {
			System.out.println("Could not load asteroid sprite.");
		}
		
		
	}
	
	public void remove(int index){
		if(index < actorList.size()){
			actorList.remove(index);
		}
	}
	
	public Actor getActor(int index){
		if(index < actorList.size()){
			return actorList.get(index);
		}
		return new Actor("null actor", null, 0, 0);
	}
	
	public void update(){
		for(int i = 0; i < actorList.size(); i++){
			getActor(i).update();
			if(actorList.get(i).getX() < -50 
					|| getActor(i).getX() > 480+70 
					|| getActor(i).getY() < -50 
					|| getActor(i).getY() > 270+50 
					|| getActor(i).getState() == -1){
				remove(i);
			}
		}
	}
	
	public int getSize(){
		return actorList.size();
	}
	
	public void dealDamage(int index, int damage){
		int newHp = actorList.get(index).getHp() - damage; 
		actorList.get(index).setHp(newHp);
	}


}
